﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web.UI;
using System.Web;
using System.Text.RegularExpressions;

namespace Ehost
{
    class XmlHelper
    {
        private XmlDocument document;
        private XmlNodeList nodeList;
        private XmlNode node;
        public List<string> lstExpected;
        public List<string> lstSteps;
        
        public void parseXml(string file)
        {
            document = new XmlDocument();
            document.Load(file);
        }
        public XmlDocument parseXmlFromString(string xml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            return doc;
        }
        public XmlNodeList getNodeList(string tag)
        {
            nodeList = document.GetElementsByTagName(tag);
            return nodeList;
        }

        public XmlNodeList getNodeListByTag(XmlNode node,string tag)
        {
            XmlDocument doc = parseXmlFromString(node.OuterXml);
            nodeList = doc.GetElementsByTagName(tag);
            return nodeList;
        }
        public XmlNodeList getNodeListByXpath(string xpath)
        {
            nodeList = document.SelectNodes(xpath);
            return nodeList;
        }

        public XmlNodeList getNodeList(XmlNode node,string xpath)
        {
            XmlDocument doc = parseXmlFromString(node.OuterXml);
            return doc.SelectNodes(xpath);
        }

        public string getAttributeValue(string xpath, string att)
        {
            string attValue = string.Empty;
            try
            {
                attValue = document.SelectSingleNode(xpath).Attributes[att].Value;
            }
            catch
            { }
            return attValue;
        }

        public string getTestSuiteName()
        {
            return getAttributeValue("//testsuite", "name");
        }

        public string getTestSuiteName(int index)
        {
            string name = "";
            try
            {
                nodeList = getNodeList("testsuite");
                name = nodeList[index].Attributes["name"].Value;
            }
            catch
            {
            }
            return name;
        }

        public string getTestLinkID(XmlNode node)
        {
            string id = string.Empty;
            try
            {
                id = node.SelectSingleNode("externalid").InnerText;
            }
            catch
            { }
            return id;
        }

        public string getSummary(XmlNode node)
        {
            string summary = string.Empty;
            try
            {
                summary = node.SelectSingleNode("summary").InnerText;
            }
            catch
            { }
            return formatString(summary);
        }

        public string getPreconditions(XmlNode node)
        {
            string value = string.Empty;
            try
            {
                value = node.SelectSingleNode("preconditions").InnerText;
            }
            catch
            { }
            return value;
        }

        public void getSteps(XmlNode node)
        {
            try
            {
                lstSteps = new List<string>();
                lstExpected = new List<string>();
                XmlNodeList steps = node.SelectSingleNode("steps").ChildNodes;
                foreach (XmlNode step in steps)
                {
                    lstSteps.Add(step.SelectSingleNode("step_number").InnerText + ". " + formatString(step.SelectSingleNode("actions").InnerText.ToString()));
                    lstExpected.Add(formatString(step.SelectSingleNode("expectedresults").InnerText));
                }
            }
            catch
            { }
     
        }

        public string formatString(string input)
        {
            string output = input.Replace("&nbsp;", string.Empty).Replace("<p>", string.Empty).Replace("</p>", string.Empty);
            try
            {
                output = HttpUtility.HtmlDecode(input);
                output = Regex.Replace(output, "<.*?>", string.Empty);
            }
            catch
            { }
            return output.Replace("&gt;", string.Empty).Replace("&lt;", string.Empty);
        }


        public string getNotes(XmlNode node)
        {
            string notes = string.Empty;
            try
            {
                XmlNodeList customFields = node.SelectSingleNode("custom_fields").ChildNodes;
                foreach (XmlNode item in customFields)
                {
                    if (item.SelectSingleNode("name").InnerText.Contains(("notes")))
                    {
                        notes = item.SelectSingleNode("value").InnerText;
                        break;
                    }
                }
            }
            catch
            { }
            return notes;
        }

        public string getJira(XmlNode node)
        {
            string jira = string.Empty;
            try
            {
                XmlNodeList customFields = node.SelectSingleNode("custom_fields").ChildNodes;
                foreach (XmlNode item in customFields)
                {
                    if (item.SelectSingleNode("name").InnerText.Contains(("CF_JIRA_REQUIREMENT")))
                    {
                        jira = item.SelectSingleNode("value").InnerText;
                        break;
                    }
                }
            }
            catch
            { }
            return jira;
        }

        public string getAutomationStatus(XmlNode node)
        {
            string status = string.Empty;
            try
            {
                XmlNodeList customFields = node.SelectSingleNode("custom_fields").ChildNodes;
                foreach (XmlNode item in customFields)
                {
                    if (item.SelectSingleNode("name").InnerText.Contains(("automation_status")))
                    {
                        status = item.SelectSingleNode("value").InnerText;
                        break;
                    }
                }
            }
            catch
            { }
            return status;
        }

        public string getApplicableInterfaces(XmlNode node)
        {
            string status = string.Empty;
            try
            {
                XmlNodeList customFields = node.SelectSingleNode("custom_fields").ChildNodes;
                foreach (XmlNode item in customFields)
                {
                    if (item.SelectSingleNode("name").InnerText.Contains(("app_interfaces")))
                    {
                        status = item.SelectSingleNode("value").InnerText;
                        status = status.Replace("|", ", ");
                        break;
                    }
                }
            }
            catch
            { }
            return status;
        }

        public string getImportant(XmlNode node)
        {
            string important = string.Empty;
            try
            {
                XmlNodeList customFields = node.SelectSingleNode("importance").ChildNodes;
               
                    important = node.SelectSingleNode("importance").InnerText;
                    if (important == "1")
                    {
                        important = "low";
                    }
                    else if (important == "2")
                    {
                        important = "medium";
                    }
                    else if (important == "3")
                    {
                        important = "high";
                    }
                    else important = "";

            }
            catch
            { }
            return important;
        }
    }
}
